package com.wc.AlgoOJ._数学.素数判定4;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/4 20:20
 * @description http://43.138.190.70:8888/p/MA0004
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = (int) 1e7 + 10;
    static int[] primes = new int[N];
    static boolean[] st = new boolean[N];
    static int[] s = new int[N];
    static int cnt = 0;

    public static void main(String[] args) {
        ola();
        for (int i = 1; i < N; i++) {
            if (st[i]) s[i] = s[i - 1];
            else s[i] = s[i - 1] + 1;
        }
        int T = sc.nextInt();
        while (T-- > 0) {
            int l = sc.nextInt(), r = sc.nextInt();
            out.println(s[r] - s[l - 1]);
        }
        out.flush();
    }

    static void ola() {
        st[0] = st[1] = true;
        for (int i = 2; i < N; i++) {
            if (!st[i]) primes[cnt++] = i;
            for (int j = 0; i <= (N - 1) / primes[j]; j++) {
                st[i * primes[j]] = true;
                if (i % primes[j] == 0) break;
            }
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
